package team.fjut.cf.service.impl;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.fjut.cf.mapper.*;
import team.fjut.cf.pojo.po.ContestRank;
import team.fjut.cf.pojo.po.JudgeStatus;
import team.fjut.cf.pojo.po.UserCustomInfo;
import team.fjut.cf.pojo.vo.response.ContestProblemVO;
import team.fjut.cf.service.ContestRankService;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author pyb [2021/3/16]
 */

@Service
public class ContestRankServiceImpl implements ContestRankService {
    @Resource
    UserCustomInfoMapper userCustomInfoMapper;

    @Resource
    ContestRankMapper contestRankMapper;

    @Resource
    JudgeStatusMapper judgeStatusMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean ContestRank(JudgeStatus TempJudgeStatus){
        //从数据库中获取本次提交状态
        JudgeStatus judgeStatus = judgeStatusMapper.selectByPrimaryKey(TempJudgeStatus);

        //判断本次提交是否属于比赛。是则进入下一步，否则结束。
        if(judgeStatus.getContestId().intValue()!=-1){
            //判断该条记录是否在t_contest_rank中被创建。是的话进入修改步骤，否的话进入创建步骤后再进入修改步骤。
            Example example1 = new Example(ContestRank.class);
            Example.Criteria criteria1 = example1.createCriteria();
            criteria1.andEqualTo("contestId" ,judgeStatus.getContestId());
            criteria1.andEqualTo("username" ,judgeStatus.getUsername());
            List<ContestRank> contestRanks = contestRankMapper.selectByExample(example1);

            if(contestRanks==null||contestRanks.size()==0){
                //创建步骤：wa和ac置为0，lasttime置为null。创建成功进入修改步骤。
                ContestRank contestRank = new ContestRank();
                contestRank.setContestId(judgeStatus.getContestId());
                contestRank.setUsername(judgeStatus.getUsername());

                //需要根据username查询昵称
                Example example2 = new Example(UserCustomInfo.class);
                Example.Criteria criteria2 = example2.createCriteria();
                criteria2.andEqualTo("username",judgeStatus.getUsername());
                UserCustomInfo userCustomInfo = userCustomInfoMapper.selectOneByExample(example2);
                contestRank.setNickname(userCustomInfo.getNickname());

                contestRank.setWrong(0);
                contestRank.setAdopt(0);
                contestRankMapper.insertSelective(contestRank);
            }


            //查询该题是否有历史通过记录。若有历史通过记录，本次提交无论对错都不对排名产生影响。
            Example example3 = new Example(JudgeStatus.class);
            Example.Criteria criteria3 = example3.createCriteria();
            criteria3.andEqualTo("contestId",judgeStatus.getContestId());
            criteria3.andEqualTo("username",judgeStatus.getUsername());
            criteria3.andEqualTo("result",1);
            List<JudgeStatus> judgeStatuses = judgeStatusMapper.selectByExample(example3);
            if(judgeStatuses==null||judgeStatuses.size()==0){
                //修改步骤：判断本次提交是wa还是ac。ac步骤和wa步骤如下。
                //获取某用户某次比赛的排名信息
                Example example4 = new Example(ContestRank.class);
                Example.Criteria criteria4 = example4.createCriteria();
                criteria4.andEqualTo("username",judgeStatus.getUsername());
                criteria4.andEqualTo("contestId",judgeStatus.getContestId());
                ContestRank contestRank = contestRankMapper.selectOneByExample(example4);

                if(judgeStatus.getResult()==1){
                    //无历史通过记录，则ac数量+1，更新最后一次有效得分时间.
                    contestRank.setAdopt(contestRank.getAdopt()+1);
                    Date currentTime = new Date();
                    contestRank.setLastTime(currentTime);
                    contestRankMapper.updateByPrimaryKey(contestRank);
                }
                else{
                    //无历史通过记录则wa的次数+1。
                    contestRank.setWrong(contestRank.getWrong()+1);
                    contestRankMapper.updateByPrimaryKey(contestRank);
                }
            }
        }
        return true;
    }

    @Override
    public List<ContestRank> GetContestRank(Integer contestId){
        return contestRankMapper.GetContestRanking(contestId);
    }


}
